package top.xierongsheng.code;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.Getter;
import lombok.ToString;

import java.util.HashMap;
import java.util.Map;

@Data
@Schema(name = "统一返回对象格式2")
public class ResultVo {

    @Schema(description = "处理状态码")
    private int code;

    @Schema(description = "返回数据对象")
    private Object data;

    @Schema(description = "处理消息")
    private String message;

    public static ResultVo ok(){
        ResultVo r = new ResultVo();
        r.setCode(State.ok.getCode());
        r.setMessage(State.ok.getMessage());
        return r;
    }

    public static ResultVo fail(){
        ResultVo r = new ResultVo();
        r.setCode(State.fail.getCode());
        r.setMessage(State.fail.getMessage());
        return r;
    }

    public static ResultVo error(){
        ResultVo r = new ResultVo();
        r.setCode(State.error.getCode());
        r.setMessage(State.error.getMessage());
        return r;
    }

    public static ResultVo authentication(){
        ResultVo r = new ResultVo();
        r.setCode(State.authentication.getCode());
        r.setMessage(State.authentication.getMessage());
        return r;
    }

    public static ResultVo authority(){
        ResultVo r = new ResultVo();
        r.setCode(State.authority.getCode());
        r.setMessage(State.authority.getMessage());
        return r;
    }

    public static ResultVo setResult(State state){
        ResultVo r = new ResultVo();
        r.setCode(state.getCode());
        r.setMessage(state.getMessage());
        return r;
    }

    public ResultVo message(String message){
        this.setMessage(message);
        return this;
    }

    public ResultVo code(Integer code){
        this.setCode(code);
        return this;
    }

    public ResultVo data(Object value){
        this.data = value;
        return this;
    }


    //状态枚举
    @Getter
    @ToString
    public enum State {
        ok(0, "操作成功"), fail(1, "操作失败"), error(500, "服务器异常,请销后重试"),
        authentication(100, "token已过期"), authority(300, "您没有此权限");

        State(Integer code, String message) {
            this.code = code;
            this.message = message;
        }

        private final Integer code;
        private final String message;

    }

}
